<?php
namespace Controllers;
use Config\Base as Config;
use DB\ArticleService;
use Models\Article;

/**
 * @author Evgeny Baranov <evgeny@baranov.eu>
 */
class Index extends Base {

    /**
     * @param array $param
     * @return array
     */
    function index(array $param) {
        $this->setView('Article');
        $as = new ArticleService();
        try {
            $list = $as->getLatest(Article::GROUP_BLOG);
        } catch (\Exception $e) {
            $list = array();
        }

        return array('list' => $list);
    }

    /**
     * @param array $param
     */
    function lang(array $param) {
        $lang = isset($param[0]) && ($lang = $param[0]) ? $lang : Config::instance()->lang->default;
        $from = \App::getLang();
        \App::setLang($lang);
        $back = isset($_SERVER['HTTP_REFERER']) && ($back = $_SERVER['HTTP_REFERER']) ? $back : '/';
        if ($lang != $from) {
            $back = \http_build_url($back, array('query' => "from={$from}"));
        }
        header('Location: ' . $back);
        exit;
    }
}
